package flight.solution;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Solution {
	
	private Map<Integer, SolutionEntry> entries;

	public Solution(Map<Integer, SolutionEntry> entries) {
		this.entries = entries;
	}
	
	public SolutionEntry getEntry(int flightHistoryId)
	{
		return entries.get(flightHistoryId);
	}
	
	public Solution(String fileName) throws IOException
	{
    	File file = new File("solutions/"+fileName);  
    	FileReader fr = new FileReader(file);    
    	BufferedReader br = new BufferedReader(fr);                                                   
    	br.readLine();
    	entries=new HashMap<Integer, SolutionEntry>();
    	String line;
    	while((line=br.readLine( )) != null)   
    	{
    		String[] c=line.split(",");
    		entries.put(Integer.parseInt(c[0]), new SolutionEntry(Integer.parseInt(c[0]), Integer.parseInt(c[1]), Integer.parseInt(c[2])));
    	}
    	br.close();
	}
	
	public void writeToFile(String fileName) throws IOException
	{
    	FileWriter outFile = new FileWriter("solutions/"+fileName);
    	PrintWriter out = new PrintWriter(outFile);

    	out.println("flight_history_id,actual_runway_arrival,actual_gate_arrival");

    	List<Integer> sortedKeys=new ArrayList(entries.keySet());
    	java.util.Collections.sort(sortedKeys);
    	Iterator<Integer> it=sortedKeys.iterator();
    	while(it.hasNext())
    	{
    		SolutionEntry entry=this.entries.get(it.next());
    		
    		out.println(entry );
    	}
    	out.close();
	}

	public Set<Integer> getFlightHistoryIds()
	{
		return this.entries.keySet();
	}

}
